package cn.agiledata.bank.common.service.email;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Map;

import cn.agiledata.bank.common.dao.ActionTypeTemplate;
import cn.agiledata.bank.common.service.NotifyMail;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.BankInfo;
import cn.agiledata.bank.hibernateBean.ScoresHis;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.Transaction;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class FillMoneyEmail extends NotifyMail {
	private String titlemessage;

	public String getTitlemessage() {
		return titlemessage;
	}

	public void setTitlemessage(String titlemessage) {
		this.titlemessage = titlemessage;
	}

	protected SubAccount setMailContext(Map vnvMap, Transaction tran,
			ScoresHis score) {
		String time = DateUtil.date2String(new Date());
		String creditName = WeatherUtil.getNameOrEmailOrMobileByAccount(tran
				.getCredit().getAccount());
		String debitName = WeatherUtil.getNameOrEmailOrMobileByAccount(tran
				.getDebit().getAccount());
		ActionTypeTemplate at = (ActionTypeTemplate) IPBAppFactory
				.getObj("actionTypeTemplate");
		SubAccount emailTo = tran.getCredit();
		vnvMap.put("name", WeatherUtil.getNameOrEmailOrMobileByAccount(emailTo
				.getAccount()));
		NumberFormat nf = new DecimalFormat("#0.00#");
		vnvMap.put("tranId", tran.getTransId());
		vnvMap.put("account", debitName);
		vnvMap.put("subaccount", emailTo.getAccountName());

		BankInfo bank = at.getBankInfoById(tran.getBankSeq().toString());
		String BankName = bank.getBankName();
		vnvMap.put("bank", BankName);
		vnvMap.put("debit", tran.getPersonName());
		vnvMap.put("trantype", "交易");
		vnvMap.put("credit", creditName);
		vnvMap.put("transtype", "非担保交易");
		String status = at.getTransStatusById(tran.getTransStatus())
				.getListName();
		vnvMap.put("transstatue", status);
		vnvMap.put("tranTime", DateUtil.date2String(tran.getBeginDate()));
		if ((!emailTo.getAccount().getAccountType().equals(
				Constant.ACCOUNT_TYPE_CORPORATION))
				&& (tran.getActionType().getId().toString()
						.equals(Constant.ACTION_TYPE_A))) {
			//本次交易所获积分
			Double ds = score.getScoresAmount();
			if (ds==null || ds.doubleValue()==0) {
				vnvMap.put("score", "0");
			} else {
				vnvMap.put("score", nf.format(ds));
			}
			
			//累积积分
			ds = tran.getCredit().getAccount().getTotalScores();
			if (ds==null || ds.doubleValue()==0) {
				vnvMap.put("totalscore", "0");
			} else {
				vnvMap.put("totalscore", nf.format(ds));
			}
		} else {
			// 企业付款，支付链非个人帐户收款
			this.setVmFile("mail-fillMoneyCompany.vm");
		}
		vnvMap.put("amount", nf.format(tran.getAmount()));
		vnvMap.put("fee", tran.getCreditFee().toString());
		double trueamount = tran.getAmount().doubleValue()
				- tran.getCreditFee().doubleValue();
		vnvMap.put("trueamount", nf.format(trueamount));
		vnvMap.put("time", time);
		vnvMap.put("titlemessage", titlemessage);
		return emailTo;
	}

}
